package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import com.elite.groovy.kh.LogToDB
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.kh.KHDBConfig
import com.elite.groovy.util.Util
import net.sf.json.JSONArray
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.springframework.jdbc.core.JdbcTemplate
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

/**
 * @projectName: ngsapitask-davco
 * @package: com.elite.groovy.callback*
 * @className: apiWoOperation*
 * @author: tian*
 * @description: 移动工单操作API*
 * @date: 2023-08-02 18:00
 * @version: 1.0
 */

class apiWoOperation implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiWoOperation.class);
    private String charset = "utf-8"; //设置字符集
    private int timeout = 100000; //设置请求超时时间
    private int enableSSLflag = 0;
    private String insertString = "";

    //主方法
    String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        // 初始化返回参数
        JSONObject result = new JSONObject();
        String re_msg = "";
        String in_operation_type = "";

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }
        log.info("[调用请求信息]:" + callJson);
        try {
            callJson = URLDecoder.decode(callJson, "UTF-8");
        } catch (Exception e) {
            log.warn("对入参报文尝试URLDecoder失败,使用原报文处理-可忽略" + e.getMessage());
        }
        log.info("[调用请求信息-转码后]:" + callJson);

        try {
            //转译
            String inJsonstr = filterJson(callJson);
            log.info("[调用请求信息-转译后]:" + inJsonstr);
            //解析Json
            JSONObject inJson = JSONObject.fromObject(inJsonstr);
            log.info("[调用请求信息-解析Json后]:" + inJson);
            //获取操作类型
            in_operation_type = inJson.getString("operation_type");
            //工单操作类型，
            String re_code = "";
            if (in_operation_type.equals("CREATE")) {
                result = CreateOrder(eliteBean, inJson, in_operation_type); //创建工单
            } else if (in_operation_type.equals("SENDNS")) {
                result = SendOrder(eliteBean, inJson, in_operation_type); //下送工单-修改
            } else if (in_operation_type.equals("SAVE")) {
                result = SaveOrder(eliteBean, inJson, in_operation_type); //暂存工单
            } else if (in_operation_type.equals("END")) {
                result = CloseOrder(eliteBean, inJson, in_operation_type); //工单结案
            } else if (in_operation_type.equals("URGE")) {
                result = UrgeOrder(eliteBean, inJson, in_operation_type);  //工单催单
            } else if (in_operation_type.equals("TRANSF")) {
                result = TransferOrder(eliteBean, inJson, in_operation_type);//工单转派-修改
            } else if (in_operation_type.equals("FAILED")) {
                result = FailOrder(eliteBean, inJson, in_operation_type);//工单撤销/关闭/取消工单
            } else if (in_operation_type.equals("B10001")) {
                result = reSubmitOrder(eliteBean, inJson, in_operation_type);//重新提交-修改
            } else if (in_operation_type.equals("B10002")) {
                result = BackOrder(eliteBean, inJson, in_operation_type);//工单退单-业务处理人
            } else if (in_operation_type.equals("APPEND")) {
                result = AppendOrder(eliteBean, inJson, in_operation_type);//追加意见
            } else {
                result.put("msg", "【处理失败】当前工单没有该操作方法");
                result.put("code", "-1");
            }
            long endTime = System.currentTimeMillis();
            if (result.get("code") == "1") {
                result.put("msg", re_msg + " " + "【处理完成】调用问题处理时长(毫秒):" + (endTime - startTime));
            }
        } catch (Exception e) {
            result.put("code", -99);
            result.put("msg", e);
            log.error("Exception Error:", e);
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "工单接口-" + in_operation_type, "apiWoOperation", callJson, result.toString(), (System.currentTimeMillis() - startTime).toString(), re_msg);
        }
        return result.toString();
    }

    //创建工单
    JSONObject CreateOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单创建逻辑---------------------");
        Map fieldValue = new HashMap<String, Object>();
        log.debug(fieldValue.toString())
        JSONObject result = new JSONObject();

        //取工单操作数据
        log.debug("解析数据workInfo：" + tabcValue.toString());
        String in_s_objective_guid = KHUtil.GetSFJV(tabcValue, "in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = KHUtil.GetSFJV(tabcValue, "in_s_elitetask_guid"); //当前elitetask_guid
        String workorderid = KHUtil.GetSFJV(tabcValue, "workorderid"); //工单号
        String operate_group = KHUtil.GetSFJV(tabcValue, "operate_group"); //操作组
        String operate_staff = KHUtil.GetSFJV(tabcValue, "operate_staff"); //操作人
        String handleby_id = KHUtil.GetSFJV(tabcValue, "handleby_id"); //下一步处理人
        String handlegroup_id = KHUtil.GetSFJV(tabcValue, "handlegroup_id"); //下一步处理组
        String comments = KHUtil.GetSFJV(tabcValue, "comments"); //操作备注
        String from_stepidmap = KHUtil.GetSFJV(tabcValue, "from_stepidmap"); //工单当前节点
        String map_id = KHUtil.GetSFJV(tabcValue, "map_id");//工单自定义流程ID
        String customer_guid = KHUtil.GetSFJV(tabcValue, "customer_guid"); //客户编号
        String proctime = KHUtil.GetSFJV(tabcValue, "proctime"); //优先级对应的天数
        String objectivesubstatus = KHUtil.GetSFJV(tabcValue, "objectivesubstatus");//工单操作状态，用于查找下个节点

        proctime = KHUtil.isNull(proctime) ? "3" : proctime;

        //取tabc数据
        String tabctablename = KHUtil.GetSFJV(tabcValue, "tabctablename"); //工单tabc表名
        JSONObject tabc_info = tabcValue.getJSONObject("workoder_content").getJSONObject("tabc_info");//获取tabc内容报文
        log.debug("解析数据tabcInfo：" + tabc_info.toString());

        String mobile = tabc_info.getString("mobile"); //手机号码
        String customername = tabc_info.getString("customername"); //客户姓名
        String priority_id = tabc_info.getString("priority"); //优先级
        String category = tabc_info.getString("category"); //工单类型
        String workordertype1 = tabc_info.getString("workordertype1"); //工单一级分类
        String workordertype2 = tabc_info.getString("workordertype2"); //工单二级分类
        String workordertype3 = tabc_info.getString("workordertype3"); //工单三级分类
        String workordertype4 = tabc_info.getString("workordertype4"); //工单四级分类
        String province = tabc_info.getString("province"); //省
        String city = tabc_info.getString("city"); //市
        String area = tabc_info.getString("area"); //区


        //处理客户资料为空，自动获取客户资料并在客服中心创建
        if (KHUtil.isNull(customer_guid)) {
            log.debug("【自动获取客户资料】-开始处理");
            //新建客户资料（接口校验是否存在客户，不存在则新建客户，返回客户编号）
            String post_body = '''{"CustomerName":"''' + customername + '''","Mobile":"''' + mobile + '''","Source":"WORK"}''';
            String custResult = Util.doHttpPost(KHDBConfig.getAPIURL(eliteBean, "KFZX_GetCustomer"), post_body, "UTF-8", 15000, 0);
            JSONObject custJson = JSONObject.fromObject(custResult);
            String re_code = custJson.getString("respCode");
            if (re_code == "200") {
                customer_guid = custJson.getJSONArray("data").getJSONObject(0).getString("customer_guid");
//获取返回结果的第一个客户编号
                log.debug("【自动获取客户资料】-处理成功，调用apiGetCustomer结果:" + customer_guid);
            } else {
                String re_msg = custJson.getString("respDesc");
                log.debug("【自动获取客户资料】-处理失败,调用apiGetCustomer结果::" + re_msg);
                result.put("code", "-1");
                result.put("msg", "【调用失败】-新建客户接口提示：" + re_msg);
                return result;
            }
        }
        log.debug("客户信息处理结果：" + customer_guid);


        //根据工单流程自动获取工单下个节点数据(工单步骤、处理人。其他暂不考虑多任务、工单时效)
        List workProcess = getWorkProcess(eliteBean, map_id, from_stepidmap, "");
        String workorderframe_id = "";
        String to_stepidmap = "";
        String handlerole_id = "";
        log.debug(workProcess.toString());
        if (workProcess.size() > 0) {
            workorderframe_id = workProcess.get(0).get("workorderframe_id");//工单模板流程ID
            to_stepidmap = workProcess.get(0).get("target_id");//工单下个步骤节点ID
            //前端未指明处理人员，如已指明则按前端派发，则由系统根据工单流程配置自动获取下个节点处理对象(包括组或人或角色)
            if (handlegroup_id == "" && handleby_id == "") {
                handlegroup_id = workProcess.get(0).get("handlegroup_id");//工单下个步骤节点处理组
                handleby_id = workProcess.get(0).get("handleby_id");//工单下个步骤节点处理人
                handlerole_id = workProcess.get(0).get("handlerole_id");//工单下个步骤节点处理角色
                log.debug("【自动获取工单节点】-前端未指明处理人自动获取工单流程配置的处理人:" + handlegroup_id + "|" + handleby_id + "|" + handlerole_id);
            }
            //如最终未能获取到合适的处理人则返回未获取到处理人结果
            if (handlegroup_id == "" && handleby_id == "" && handlerole_id == "") {
                result.put("code", "-1");
                result.put("msg", "未找到适合的工单处理人【" + map_id + "】【" + from_stepidmap + "】");
                log.debug("【自动获取工单步骤】-处理失败，未找到适合的工单处理人【" + map_id + "】【" + from_stepidmap + "】");
                return result;
            }
            log.debug("【自动获取工单节点】-处理完成" + workorderframe_id + "|" + to_stepidmap + "|" + handlegroup_id + "|" + handleby_id + "|" + handlerole_id);
        } else {
            result.put("code", "-1");
            result.put("msg", "未获取到下个处理节点信息【" + map_id + "】【" + from_stepidmap + "】");
            log.debug("【自动获取工单节点】-处理失败，未查询到到下个处理节点");
            return result;
        }
        log.debug("工单流程获取结果：" + workorderframe_id + "|" + to_stepidmap + "|" + handlerole_id + "|" + handleby_id);

        //取需修改tabc字段
        log.debug("【传入TABC字段拼接】-开始");
        JSONObject tabcJSON = tabcValue.getJSONObject("workoder_content").getJSONObject("tabc_info");
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = "";
        int i = 0;
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            log.debug("key:" + key);
            if (fieldName == "") {
                fieldName = key;
                valueString = "?";
                updateString = key + "=" + "?";
                insertString = key;
            } else {
                fieldName = fieldName + "," + key;
                valueString = valueString + ",?";
                updateString = updateString + "," + key + "=" + "?";
                insertString = insertString + "," + key;
            }

            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : "'" + fieldValue.get(key) + "'");
            } else {
                valueList.add("'" + fieldValue.get(key) + "'");
            }
            i++;
        }
        if (fieldName == "") {
            log.debug("【获取待修改tabc字段-失败】tabc字段不能为空");
            result.put("code", "-1");
            result.put("msg", "tabc字段不能为空");
            return result;
        }
        String valueDtring = valueList.join(",");
        log.debug("【传入TABC字段拼接】-表字段内容：" + insertString);
        log.debug("【传入TABC字段拼接】-值内容：" + valueDtring);

        //获取优先级对应的截止时间
        String expireddate = "";
        String getPriporySql = "select r1.d_date as freedate from (select distinct r1.d_date from dim_d_day r1 left join c_freedate r2 on date_format(r2.freedate, '%Y-%m-%d') = r1.d_date and r2.datetype != '000010' where r1.d_date >= date_format(now(), '%Y-%m-%d') and r2.freedate is null  order by r1.d_date limit " + proctime + ") r1 order by r1.d_date desc limit 1";
        List priporyList = eliteBean.chaxun_sql(getPriporySql, "dataSource");
        if (priporyList.size() > 0) {
            expireddate = priporyList.get(0).get("freedate");
        }

        //数组提交数据存储
        log.debug("【执行工单创建SQL】-开始执行");
        String[] data1 = new String[5];
        data1[0] = "insert into " + tabctablename + "(" + insertString + ",createddate,MODIFIEDDATE,createdby,MODIFIEDBY,customer_guid,workorderid) select " + valueDtring + ",now(),now(),'" + operate_staff + "','" + operate_staff + "','" + customer_guid + "','" + workorderid + "'";
        data1[1] = "INSERT INTO objective(OBJECTIVE_GUID, OBJECTIVETYPE_ID, HANDLEBY_ID, HANDLEGROUP_ID, PRIORITY_ID, EXPIREDDATE, CUSTOMER_GUID, SUBSCRIBER_GUID, ACCOUNT_GUID, FIRSTCONTACTDATE, LASTCONTACTDATE, OBJECTIVESTATUS, ISCONTACT, TRYTIMES, SUCCESSFULCONTACTTIMES, USERDEFINEDSTATUS, COMMENTS, STRINGFIELD1, STRINGFIELD2, CREATEDBY, CREATEDDATE, MODIFIEDBY, MODIFIEDDATE, CALLRESULT_ID, CALLRESULTREASON_ID, PREOBJECTIVE_GUID, EVENT_GUID, EXPECTTRADEDATE, FAILREASON, QUANTITY, AMOUNT, OBDNIS, OBSTATUS, OBSTARTTIME, OBENDTIME, OBHANDLEGROUP, OBHANDLEBY, CUSTLISTNAME, CONTACTPHONE, OBJECTIVETRYSTATUS, LAST1EVENT_GUID, LAST2EVENT_GUID, LAST3EVENT_GUID, LAST4EVENT_GUID, LAST5EVENT_GUID, DEALGROUP_ID, DATATYPE, FIRSTCALLDATE, FAILURE_ATTEMPT, CONTACT_CUSTOMER_GUID, PASSFLAG, CUSTOMERLEVEL, LOCALTRYCOUNT, LASTASSIGNDATE, FAILSTATUS, SUCCESSSTATUS, TEMPLABLE, ISANSWER, ORDERSTATUS, BRIEF, PREPOSSIBILITY, EXPECTSTARTTIME, OWNER, WORKORDERFRAME_ID, CATEGORY, EXPIREDHANDLEGROUP_ID, EXPIREDHANDLEBY_ID, STRINGFIELD3, STRINGFIELD4, STRINGFIELD5, FORECASTDATE, WORKORDERID, WORKORDERTYPE1, WORKORDERTYPE2, WORKORDERTYPE3, WORKORDERTYPE4, FLOWDETAIL, ANSWERMETHOD, OBJECTIVESUBSTATUS, MAP_ID, OBJECTIVESUBSTATUS2, OBJECTIVESUBSTATUS3, CHANNEL, CURSTEP_ID_MAP, LASTCALLDATE, QA_FLAG, DIVISION_ID, EXPECTENDTIME, MAX_ATTEMPT, NOCONTACTTRY, HANDLEROLE_ID, to_putoff, soure_path, rolegroup_id, customernameabb) VALUES ('" + in_s_objective_guid + "', '" + workorderframe_id + "', '" + handleby_id + "', '" + handlegroup_id + "', '" + priority_id + "', '" + expireddate + "', '" + customer_guid + "', NULL, NULL, NULL, NULL, 'OPEN', NULL, NULL, NULL, NULL, '" + comments + "', NULL, NULL, '" + operate_staff + "', now(), '" + operate_staff + "', now(), NULL, NULL, NULL, NULL, NULL, NULL, null, null, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '" + workorderframe_id + "', '" + category + "', NULL, NULL, NULL, NULL, NULL, now(), '" + workorderid + "', '" + workordertype1 + "', '" + workordertype2 + "', '" + workordertype3 + "', '" + workordertype4 + "', NULL, NULL, 'OPEN', '" + map_id + "', NULL, NULL, NULL, '" + to_stepidmap + "', NULL, NULL, NULL, NULL, NULL, NULL, '" + handlerole_id + "', NULL, '1', '', NULL)";
        data1[2] = "INSERT INTO elitetask(ELITETASK_GUID, OBJECTIVE_GUID, OBJECTIVETYPE_ID, EXPECTSTARTTIME, EXPECTENDTIME, LASTCALLTIME, OBSTATUS, RECORDSTATUS, ATTEMPT, MAX_ATTEMPT, PRIORITY, HANDLEBY_ID, HANDLEGROUP_ID, CREATEDBY, CREATEDDATE, MODIFIEDBY, MODIFIEDDATE, LASTSTATUS, CONTACTPHONE, EVENT_GUID, CUSTLISTNAME, MYSELF, CURRENTPHONEATTEMPTS, PHONE, PHONEINDEX, ORIGINOBSTATUS, CURRENTEXPECTSTARTTIME, LASTCHAINTIME, FAILURE_ATTEMPT, USERDEFINEDSTATUS, COMMENTS, CUSTOMER_GUID, CUSTOMERLEVEL, LOCALTRYCOUNT, LASTASSIGNDATE, WORKORDERFRAME_ID, SUBSCRIBER_GUID, ACCOUNT_GUID, CURSTEPSTATUS, CURSTEP_ID, EXPIREDHANDLEGROUP_ID, EXPIREDHANDLEBY_ID, STEPFORECASTDATE, FLOWDETAIL, ANSWERMETHOD, CURSTEP_ID_MAP, MAP_ID, OBJECTIVESUBSTATUS, ELITETASKSTATUS, OBFLAG, COMMENTS_R_PATH, HANDLEROLE_ID, MAINTASKSTEP_ID_MAP) VALUES ('" + in_s_elitetask_guid + "', '" + in_s_objective_guid + "', '" + workorderframe_id + "', now(), null, NULL, NULL, NULL, NULL, NULL, '" + priority_id + "', '" + handleby_id + "', '" + handlegroup_id + "',  '" + operate_staff + "', now(), '" + operate_staff + "', now(), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '" + comments + "', '" + customer_guid + "', NULL, null, NULL, '" + workorderframe_id + "', NULL, NULL, 'OPEN', '" + to_stepidmap + "', null, null, null, NULL, NULL, '" + to_stepidmap + "', '" + map_id + "', 'OPEN', NULL, 0, NULL, '" + handlerole_id + "', NULL)";
        data1[3] = "INSERT INTO workorderstep3(WORKORDER_GUID, WORKORDERSTEP_GUID, OLDHANDLEBY_ID, OLDHANDLEGROUP_ID, HANDLEBY_ID, HANDLEGROUP_ID, WORKORDERSTEPSTATUS, RELATEDJOB_GUID, RELATEDJOB2_GUID, RELATEDMODULETYPE, WORKORDERSTEPJOBSTATUS, WAITEXPIRED, COMMENTS, MODIFIEDDATE, WORKORDERSTEPID, STEPEXPIRED, CHILD_ID, STEPSTART, CUR_EXPIREDFLAG, CUR_EXPIREDLEN, EVENT_GUID, FROM_STEPIDMAP, TO_STEPIDMAP, WORKSTARTTIME, WORKENDTIME, COMMENTS_R_PATH, HANDLEROLE_ID, OLDHANDLEROLE_ID, handle_staff_id) VALUES ('" + in_s_objective_guid + "', uuid(), '" + operate_staff + "', '" + operate_group + "', '" + handleby_id + "', '" + handlegroup_id + "', 'SENDNS', NULL, NULL, NULL, NULL, NULL, '" + comments + "', now(), NULL, null, '手机端', NULL, null, null, null, '" + from_stepidmap + "', '" + to_stepidmap + "', now(), now(), NULL, '" + handlerole_id + "', NULL, NULL)";
        data1[4] = "INSERT INTO workorderpath3(WORKORDER_GUID, TOSTEPID, HANDLEGROUP, HANDLEBY, MODIFIEDDATE, FROMSTEPID, WORKORDERPATH_GUID, WOFRAME_ID, FROMFRAMEID, FROM_STEPIDMAP, TO_STEPIDMAP, HANDLEROLE_ID, FROM_STEPIDMAP_BAK) VALUES ('" + in_s_objective_guid + "', NULL, '" + handlegroup_id + "', '" + handleby_id + "', now(), NULL, uuid(), '" + map_id + "', '', '" + from_stepidmap + "', '" + to_stepidmap + "', '" + handlerole_id + "', NULL)";
        log.debug("【执行工单创建SQL-结束】:" + data1.toString());
        int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");
        log.debug("【执行工单创建SQL】-开始执行" + dataresult1);

        createMessageTask(eliteBean, in_s_objective_guid, handleby_id, handlerole_id, operation_type, "", operate_staff, map_id, province, city, area);

        result.put("code", "1");
        result.put("msg", "提交成功");
        return result;
    }

    //下送工单
    JSONObject SendOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入下送逻辑---------------------");
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();

        //取tabc表存储（工单相关的业务信息都用此表）
        String tabctablename = tabcValue.getString("tabctablename"); //工单tabc表名
        //取主键
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getString("in_s_elitetask_guid"); //当前elitetask_guid
        String workorderid = tabcValue.getString("workorderid"); //工单号
        //取工单操作信息
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人
        String handleby_id = tabcValue.getString("handleby_id"); //下一步处理人
        String handlegroup_id = tabcValue.getString("handlegroup_id"); //下一步处理组
        String comments = tabcValue.getString("comments"); //操作备注
        String priority_id = tabcValue.getString("priority_id"); //优先级
        String customer_guid = tabcValue.getString("customer_guid"); //客户编号
        //取工单当前状态
        String category = tabcValue.getString("category"); //工单类型
        String from_stepidmap = tabcValue.getString("from_stepidmap"); //工单当前节点
        String map_id = tabcValue.getString("map_id");//工单自定义流程ID
        String objectivesubstatus = tabcValue.getString("objectivesubstatus");//工单操作状态，用于查找下个节点

        //自动获取工单流程信息(暂不考虑多任务、工单时效)
        List workProcess = getWorkProcess(eliteBean, map_id, from_stepidmap, objectivesubstatus);
        String workorderframe_id = "";
        String to_stepidmap = "";
        String s2_nodemap = "";
        if (workProcess.size() > 0) {
            workorderframe_id = workProcess.get(0).get("workorderframe_id");//工单模板流程ID
            to_stepidmap = workProcess.get(0).get("target_id");//工单下个步骤节点
            s2_nodemap = workProcess.get(0).get("s2_nodemap");//是否业务处理节点
            if (to_stepidmap == "END") {
                result.put("code", "-1");
                result.put("msg", "当前节点为结案节点，无法下送【" + map_id + "】【" + to_stepidmap + "】");
                return result;
            }
        } else {
            result.put("code", "-1");
            result.put("msg", "未获取到下个处理步骤【" + map_id + "】【" + from_stepidmap + "】");
            return result;
        }

        //取需修改tabc字段
        JSONObject tabcJSON = tabcValue.getJSONObject("tabc_info");
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = "";
        int i = 0;
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            log.debug("key:" + key);
            if (!KHUtil.isNull(String.valueOf(fieldValue.get(key)))) {
                updateString = updateString + "," + key + "=" + "'" + fieldValue.get(key) + "'";
                insertString = insertString + "," + key;
            }

            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add("'" + fieldValue.get(key) + "'");
            }
            i++;
        }

        updateString = updateString.replaceFirst(",", "");
        insertString = insertString.replaceFirst(",", "");

        log.debug("updateString:" + updateString);
        log.debug("insertString:" + insertString);

        String whereSql = "";
        if (!KHUtil.isNull(s2_nodemap)) {
            whereSql = ",STRINGFIELD2='" + handleby_id + "'";
        }

        //数组提交数据存储
        String[] data1 = new String[6];

        data1[0] = "update objective set handleby_id = '" + handleby_id + "',handlegroup_id = '" + handlegroup_id + "',comments = '" + comments + "',modifiedby = '" + operate_staff + "',modifieddate = now(),curstep_id_map = '" + to_stepidmap + "' " + whereSql + " where objective_guid = '" + in_s_objective_guid + "'";
        data1[1] = "update elitetask set handleby_id = '" + handleby_id + "',handlegroup_id = '" + handlegroup_id + "',modifiedby = '" + operate_staff + "',modifieddate = now(),comments = '" + comments + "',curstep_id = '" + to_stepidmap + "',curstep_id_map = '" + to_stepidmap + "' where elitetask_guid = '" + in_s_elitetask_guid + "'";
        data1[2] = "INSERT INTO workorderstep3(WORKORDER_GUID, WORKORDERSTEP_GUID, OLDHANDLEBY_ID, OLDHANDLEGROUP_ID, HANDLEBY_ID, HANDLEGROUP_ID, WORKORDERSTEPSTATUS, RELATEDJOB_GUID, RELATEDJOB2_GUID, RELATEDMODULETYPE, WORKORDERSTEPJOBSTATUS, WAITEXPIRED, COMMENTS, MODIFIEDDATE, WORKORDERSTEPID, STEPEXPIRED, CHILD_ID, STEPSTART, CUR_EXPIREDFLAG, CUR_EXPIREDLEN, EVENT_GUID, FROM_STEPIDMAP, TO_STEPIDMAP, WORKSTARTTIME, WORKENDTIME, COMMENTS_R_PATH, HANDLEROLE_ID, OLDHANDLEROLE_ID, handle_staff_id) VALUES ('" + in_s_objective_guid + "', uuid(), '" + operate_staff + "', '" + operate_group + "', '" + handleby_id + "', '" + handlegroup_id + "', 'SENDNS', NULL, NULL, NULL, NULL, NULL, '" + comments + "', now(), NULL, null,'手机端', NULL, null, null, null, '" + from_stepidmap + "', '" + to_stepidmap + "', now(), now(), NULL, NULL, NULL, NULL)";
        data1[3] = "INSERT INTO workorderpath3(WORKORDER_GUID, TOSTEPID, HANDLEGROUP, HANDLEBY, MODIFIEDDATE, FROMSTEPID, WORKORDERPATH_GUID, WOFRAME_ID, FROMFRAMEID, FROM_STEPIDMAP, TO_STEPIDMAP, HANDLEROLE_ID, FROM_STEPIDMAP_BAK) VALUES ('" + in_s_objective_guid + "', NULL, '" + handlegroup_id + "', '" + handleby_id + "', now(), NULL, uuid(), '" + map_id + "', '', '" + from_stepidmap + "', '" + to_stepidmap + "', NULL, NULL)";
        data1[4] = "update " + tabctablename + " set " + updateString + " where relation_id='" + in_s_objective_guid + "'";
        data1[5] = "insert into tabcworkorder_his select * from tabcworkorder where relation_id='" + in_s_objective_guid + "'";


        log.debug("保存数据提交结果:" + data1.toString());
        int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

        createMessageTask(eliteBean, in_s_objective_guid, handleby_id, "", operation_type, "", operate_staff, "", "", "", "");

        result.put("code", "1");
        result.put("msg", "提交成功")
        return result;

    }

    //保存工单- 未启用
    JSONObject SaveOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单修改逻辑---------------------");
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();
        //取tabc表存储
        String tabctablename = tabcValue.getString("tabctablename"); //工单tabc表名
        //取主键
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getString("in_s_elitetask_guid"); //当前elitetask_guid
        //取操作信息
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人
        String comments = tabcValue.getString("comments"); //操作备注

        //取需修改tabc字段
        JSONObject tabcJSON = tabcValue.getJSONObject("tabc_info");
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = "";
        int i = 0;
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            log.debug("key:" + key);
            if (fieldName == "") {
                fieldName = key;
                valueString = "?";
                updateString = key + "=" + "?";
                insertString = key;
            } else {
                fieldName = fieldName + "," + key;
                valueString = valueString + ",?";
                updateString = updateString + "," + key + "=" + "?";
                insertString = insertString + "," + key;
            }
            //valueList.add(fieldValue.get(key));
            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add(fieldValue.get(key));
            }
            i++;
        }
        if (fieldName == "") {
            result.put("code", "-1");
            result.put("msg", "tabc字段不能为空")
            return result;
        }
        //检查是否存在工单
        List ordernums = eliteBean.chaxun_sql("select objective_guid nums from objective where objective_guid='" + in_s_objective_guid + "'", "dataSource");

        if (ordernums.size() == 0) {
            result.put("code", "-2");
            result.put("msg", "未找到工单：" + in_s_objective_guid);
            return result;
        } else {
            //数组提交数据存储
            String[] data1 = new String[5];

            data1[0] = "insert into " + tabctablename + "_his (hisguid," + insertString + ") select uuid()," + insertString + " from " + tabctablename + " t where RELATION_ID='" + in_s_objetive_guid + "'";
            data1[1] = "update " + tabctablename + " set " + updateString + " where relation_id='" + in_s_objetive_guid + "'";
            data1[2] = "Update Objective Set Modifieddate = now(),MODIFIEDBY='" + operate_staff + "' Where Objective_Guid = '" + in_s_objective_guid + "'";
            data1[3] = "Update Elitetask Set Modifieddate = now(),MODIFIEDBY='" + operate_staff + "' Where elitetask_guid = '" + in_s_elitetask_guid + "'";
            data1[4] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workstarttime, Workendtime,CHILD_ID) Values ('" + in_s_objetive_guid + "', uuid(), '" + operate_staff + "', '" + operate_group + "', 'CLOSE', '" + comments + "', now(), now(), now(),'手机端')";

            log.debug("保存数据提交结果:" + data1.toString());
            int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

            result.put("code", "1");
            result.put("msg", "保存成功")
            return result;
        }
    }

    //工单结案
    JSONObject CloseOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单结案逻辑---------------------");
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();
        //取tabc表存储
        String tabctablename = tabcValue.getString("tabctablename"); //工单tabc表名
        //取主键
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getString("in_s_elitetask_guid"); //当前elitetask_guid
        //取操作信息
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人
        String comments = tabcValue.getString("comments"); //操作备注
        String objectivesubstatus = tabcValue.getString("objectivesubstatus");//工单操作状态，用于查找下个节点

        //取需修改tabc字段
        JSONObject tabcJSON = tabcValue.getJSONObject("tabc_info");
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = "";
        int i = 0;
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            log.debug("key:" + key);
            if (!KHUtil.isNull(String.valueOf(fieldValue.get(key)))) {
                updateString = updateString + "," + key + "=" + "'" + fieldValue.get(key) + "'";
                insertString = insertString + "," + key;
            }

            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add("'" + fieldValue.get(key) + "'");
            }
            i++;
        }

        updateString = updateString.replaceFirst(",", "");
        insertString = insertString.replaceFirst(",", "");

        log.debug("updateString:" + updateString);
        log.debug("insertString:" + insertString);

        if (updateString == "") {
            result.put("code", "-1");
            result.put("msg", "tabc字段不能为空")
            return result;
        }
        //检查是否存在工单
        List ordernums = eliteBean.chaxun_sql("select objective_guid nums,map_id from objective where objective_guid='" + in_s_objective_guid + "'", "dataSource");

        if (ordernums.size() == 0) {
            result.put("code", "-2");
            result.put("msg", "未找到工单：" + in_s_objective_guid);
            return result;
        } else {

            String map_id = ordernums.get(0).get("map_id")
            //数组提交数据存储
            String[] data1 = new String[5];

            data1[0] = "insert into " + tabctablename + "_his (hisguid," + insertString + ") select uuid()," + insertString + " from " + tabctablename + " t where RELATION_ID='" + in_s_objective_guid + "'";
            data1[1] = "update " + tabctablename + " set " + updateString + " where relation_id='" + in_s_objective_guid + "'";
            data1[2] = "Update Objective Set Modifieddate = now(),MODIFIEDBY='" + operate_staff + "',COMMENTS='" + comments + "',OBJECTIVESTATUS='CLOSE',STRINGFIELD3='" + operate_staff + "' Where Objective_Guid = '" + in_s_objective_guid + "'";
            data1[3] = "Delete from  Elitetask  Where elitetask_guid = '" + in_s_elitetask_guid + "'";
            data1[4] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workstarttime, Workendtime,CHILD_ID) Values ('" + in_s_objective_guid + "', uuid(), '" + operate_staff + "', '" + operate_group + "', 'END', '" + comments + "', now(), now(), now(),'手机端')";

            log.debug("保存数据提交结果:" + data1.toString());
            int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

            //工单类型不能为空，按工单类型通知固定的预审角色，假货举报、华南市场业务工单、线索举报、防伪异常无需处理。
            if (!KHUtil.isNull(map_id) && map_id != "1EMZZA" && map_id != "0N6WDG" && map_id != "2DI8VY" && map_id != "SBMTFH") {
                String business_type = "";
                //开店咨询
                if (map_id == "THZ9L7") {
                    business_type = "BWUT04";
                }
                //扫码红包
                if (map_id == "1NK5L2") {
                    business_type = "C00001";
                }
                //技术咨询
                if (map_id == "YRYGQ3") {
                    business_type = "BWUT02";
                }
                //投诉与建议
                if (map_id == "M6AP7I") {
                    business_type = "BWUT05";
                }
                //经销商加盟
                if (map_id == "2YD66W") {
                    business_type = "BWUT03";
                }
                //销售咨询
                if (map_id == "UA9990") {
                    business_type = "BWUT01";
                }
                //预约施工
                if (map_id == "1MJ205") {
                    business_type = "BWUT09";
                }
                createMessageTask(eliteBean, in_s_objective_guid, "", business_type, operation_type, "", operate_staff, "", "", "", "");
            }


            // 判断是否是防伪反馈(防伪反馈通知客户)
            if (map_id == "1EMZZA") {
                JSONObject callBody = new JSONObject();
                callBody.put("op_type", "防伪异常反馈结果");
                callBody.put("objective_guid", in_s_objective_guid);
                log.debug("防伪反馈通知客户callBody:" + callBody);

                String callRes = Util.doHttpPost(KHDBConfig.getAPIURL(eliteBean, "KFZX_WorkMessage"), callBody.toString(), "UTF-8", 5000, 0);
                log.debug("防伪反馈通知客户callRes:" + callRes);
            }

            result.put("code", "1");
            result.put("msg", "结案成功")
            return result;
        }
    }

    //催单
    JSONObject UrgeOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单催单逻辑---------------------");
        JSONObject result = new JSONObject();

        //取主键
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid

        //查询当前数据信息
        List workinfolist = eliteBean.chaxun_sql("select handleby_id,handlegroup_id,MAP_ID,CURSTEP_ID_MAP from objective where objective_guid='" + in_s_objective_guid + "'", "dataSource")
        //当前工单信息
        String handleby_id = workinfolist.get(0).get("handleby_id"); //转派人

        //取操作信息
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人
        String comments = tabcValue.getString("comments"); //操作备注
        String objectivesubstatus = tabcValue.getString("objectivesubstatus");//工单操作状态，用于查找下个节点

        eliteBean.update("Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, Workstarttime, Workendtime,CHILD_ID) Values ('" + in_s_objective_guid + "', uuid(), '" + operate_staff + "', '" + operate_group + "', 'URGE', '" + comments + "', now(),now(), now(),'手机端')", "dataSource");

        createMessageTask(eliteBean, in_s_objective_guid, handleby_id, "", operation_type, comments, operate_staff, "", "", "", "");

        result.put("code", "1");
        result.put("msg", "催单成功")


        return result;
    }

    //正常退回
    JSONObject BackOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单退单逻辑---------------------");
        JSONObject result = new JSONObject();
        //取主键
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getString("in_s_elitetask_guid"); //当前elitetask_guid
        //取操作信息
        String comments = tabcValue.getString("comments"); //操作备注
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人
        String handleby_id = tabcValue.getString("handleby_id"); //给处理人
        String handlegroup_id = tabcValue.getString("handlegroup_id"); //给处理组
        //查询当前数据信息
        List workinfolist = eliteBean.chaxun_sql("select handleby_id,handlegroup_id,MAP_ID,CURSTEP_ID_MAP from objective where objective_guid='" + in_s_objective_guid + "'", "dataSource")
        //当前工单信息
        if (workinfolist.size() > 0) {
            String[] data1 = new String[3];
            data1[0] = "update objective set objectivestatus='OPEN',HANDLEBY_ID='" + handleby_id + "',HANDLEGROUP_ID='" + handlegroup_id + "',OBJECTIVESUBSTATUS='OPEN',STRINGFIELD2='" + handleby_id + "',STRINGFIELD3=null  where objective_guid='" + in_s_objective_guid + "'";
            data1[1] = "INSERT INTO elitetask (ELITETASK_GUID,OBJECTIVE_GUID,OBJECTIVETYPE_ID,PRIORITY,HANDLEBY_ID,HANDLEGROUP_ID,CREATEDBY,CREATEDDATE,MODIFIEDBY,MODIFIEDDATE,COMMENTS,CUSTOMER_GUID,WORKORDERFRAME_ID,CURSTEPSTATUS,CURSTEP_ID,CURSTEP_ID_MAP,MAP_ID,OBJECTIVESUBSTATUS,HANDLEROLE_ID) SELECT uuid(),o.objective_guid,o.OBJECTIVETYPE_ID,o.priority_id,'" + handleby_id + "','" + handlegroup_id + "',o.CREATEDBY,o.CREATEDDATE,o.MODIFIEDBY,o.MODIFIEDDATE,o.COMMENTS,o.CUSTOMER_GUID,o.WORKORDERFRAME_ID,o.objectivesubstatus,o.curstep_id_map,o.curstep_id_map,o.map_id,o.objectivesubstatus,o.HANDLEROLE_ID FROM OBJECTIVE o WHERE o.OBJECTIVE_GUID = '" + in_s_objective_guid + "'";
            data1[2] = "INSERT INTO workorderstep3 (WORKORDER_GUID,WORKORDERSTEP_GUID,OLDHANDLEBY_ID,OLDHANDLEGROUP_ID,HANDLEBY_ID,HANDLEGROUP_ID,WORKORDERSTEPSTATUS,COMMENTS,MODIFIEDDATE,FROM_STEPIDMAP,TO_STEPIDMAP) select o.objective_guid,uuid(),'" + operate_staff + "','" + operate_group + "','" + handleby_id + "','" + handlegroup_id + "','" + operation_type + "','" + comments + "',now(),o.curstep_id_map,o.curstep_id_map from objective o where objective_guid='" + in_s_objective_guid + "'";
            log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
            int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

            createMessageTask(eliteBean, in_s_objective_guid, handleby_id, "", operation_type, "", operate_staff, "", "", "", "");


            result.put("code", "1");
            result.put("msg", "退回成功");
        }
        return result;
    }

    //追加意见
    JSONObject AppendOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单追加意见逻辑---------------------");
        JSONObject result = new JSONObject();

        //取主键
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getString("in_s_elitetask_guid"); //当前elitetask_guid
        //取操作信息

        String comments = tabcValue.getString("comments"); //操作备注
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人

        //查询当前数据信息
        List workinfolist = eliteBean.chaxun_sql("select handleby_id,handlegroup_id,MAP_ID,CURSTEP_ID_MAP from objective where objective_guid='" + in_s_objective_guid + "'", "dataSource")
        //当前工单信息
        String CURSTEP_ID_MAP = workinfolist.get(0).get("CURSTEP_ID_MAP");

        //执行追加意见
        String AppendOrderSQL = "INSERT INTO workorderstep3(WORKORDER_GUID, WORKORDERSTEP_GUID, OLDHANDLEBY_ID, OLDHANDLEGROUP_ID, HANDLEBY_ID, HANDLEGROUP_ID, WORKORDERSTEPSTATUS, RELATEDJOB_GUID, RELATEDJOB2_GUID, RELATEDMODULETYPE, WORKORDERSTEPJOBSTATUS, WAITEXPIRED, COMMENTS, MODIFIEDDATE, WORKORDERSTEPID, STEPEXPIRED, CHILD_ID, STEPSTART, CUR_EXPIREDFLAG, CUR_EXPIREDLEN, EVENT_GUID, FROM_STEPIDMAP, TO_STEPIDMAP, WORKSTARTTIME, WORKENDTIME, COMMENTS_R_PATH, HANDLEROLE_ID, OLDHANDLEROLE_ID, handle_staff_id, is_follow) VALUES ('" + in_s_objective_guid + "' ,uuid(), '" + operate_staff + "', '" + operate_group + "', '', '', 'APPEND', NULL, NULL, NULL, NULL, NULL, '" + comments + "', now(), NULL, NULL,'手机端', NULL, NULL, NULL, NULL, '" + CURSTEP_ID_MAP + "', '" + CURSTEP_ID_MAP + "', now(), now(), NULL, '', NULL, NULL, NULL)";
        eliteBean.execute(AppendOrderSQL, "dataSource");

        result.put("code", "1");
        result.put("msg", "提交成功")
        return result;
    }

    //转派
    JSONObject TransferOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单催单逻辑---------------------");
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();

        //取主键
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getString("in_s_elitetask_guid"); //当前elitetask_guid
        //取操作信息

        String tabctablename = tabcValue.getString("tabctablename"); //工单tabc表名
        String comments = tabcValue.getString("comments"); //操作备注
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人
        String handleby_id = tabcValue.getString("handleby_id"); //转派人
        String handlegroup_id = tabcValue.getString("handlegroup_id"); //转派组

        //取需修改tabc字段
        JSONObject tabcJSON = tabcValue.getJSONObject("tabc_info");
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = "";
        int i = 0;
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            log.debug("key:" + key);
            if (!KHUtil.isNull(String.valueOf(fieldValue.get(key)))) {
                updateString = updateString + "," + key + "=" + "'" + fieldValue.get(key) + "'";
                insertString = insertString + "," + key;
            }

            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add("'" + fieldValue.get(key) + "'");
            }
            i++;
        }

        updateString = updateString.replaceFirst(",", "");
        insertString = insertString.replaceFirst(",", "");

        log.debug("updateString:" + updateString);
        log.debug("insertString:" + insertString);

        //查询当前数据信息
        List workinfolist = eliteBean.chaxun_sql("select o.handleby_id,o.handlegroup_id,o.MAP_ID,o.CURSTEP_ID_MAP,e.EXTENDVALUE from objective o left join wo3_nodemap_extend e on o.MAP_ID=e.MAP_ID and e.NODEMAP_ID=o.CURSTEP_ID_MAP and EXTENDID='nodetype' and EXTENDVALUE='S2' where o.objective_guid='" + in_s_objective_guid + "'", "dataSource")
        //当前工单信息
        if (workinfolist.size() > 0) {
            String MAP_ID = workinfolist.get(0).get("MAP_ID");
            String CURSTEP_ID_MAP = workinfolist.get(0).get("CURSTEP_ID_MAP");
            String EXTENDVALUE = workinfolist.get(0).get("EXTENDVALUE");
            String whereSql = "";
            if (EXTENDVALUE == "S2") {
                whereSql = ",STRINGFIELD2='" + handleby_id + "'";
            }
            String[] data1 = new String[5];
            data1[0] = "Update Objective Set Modifiedby = '" + operate_staff + "', Modifieddate = now(), handleby_id='" + handleby_id + "', handlegroup_id='" + handlegroup_id + "' " + whereSql + " Where Objective_Guid = '" + in_s_objective_guid + "'";
            data1[1] = "Update elitetask Set Modifiedby = '" + operate_staff + "', Modifieddate = now(), handleby_id='" + handleby_id + "', handlegroup_id='" + handlegroup_id + "', handlerole_id=null Where elitetask_guid = '" + in_s_elitetask_guid + "'";
            data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id,handleby_id,handlegroup_id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, TO_STEPIDMAP, Workstarttime, Workendtime,CHILD_ID) " + "Values ('" + in_s_objective_guid + "', uuid(), '" + operate_staff + "', '" + operate_group + "','" + handleby_id + "','" + handlegroup_id + "','TRANSF', '" + comments + "', now(), '" + CURSTEP_ID_MAP + "', '" + CURSTEP_ID_MAP + "', now(), now(),'手机端')";
            data1[3] = "update " + tabctablename + " set " + updateString + " where relation_id='" + in_s_objective_guid + "'";
            data1[4] = "insert into tabcworkorder_his select * from tabcworkorder where relation_id='" + in_s_objective_guid + "'";


            log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
            int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

            createMessageTask(eliteBean, in_s_objective_guid, handleby_id, "", operation_type, "", operate_staff, "", "", "", "");
        }
        result.put("code", "1");
        result.put("msg", "转派成功")
        return result;
    }

    //撤销/关闭/取消工单
    JSONObject FailOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单撤回逻辑---------------------");
        JSONObject result = new JSONObject();

        //取主键
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getString("in_s_elitetask_guid"); //当前elitetask_guid
        //取操作信息

        String comments = tabcValue.getString("comments"); //操作备注
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人

        //查询当前数据信息
        List workinfolist = eliteBean.chaxun_sql("select handleby_id,handlegroup_id,MAP_ID,CURSTEP_ID_MAP from objective where objective_guid='" + in_s_objective_guid + "'", "dataSource")
        //当前工单信息
        if (workinfolist.size() > 0) {
            String MAP_ID = workinfolist.get(0).get("MAP_ID");
            String CURSTEP_ID_MAP = workinfolist.get(0).get("CURSTEP_ID_MAP");

            String[] data1 = new String[3];
            data1[0] = "Update Objective Set Modifiedby = '" + operate_staff + "', Modifieddate = now(),objectivestatus = 'FAIL',OBJECTIVESUBSTATUS='FA0000'  Where Objective_Guid = '" + in_s_objective_guid + "'";
            data1[1] = "Delete from  Elitetask  Where elitetask_guid = '" + in_s_elitetask_guid + "'";
            data1[2] = "Insert Into Workorderstep3 (Workorder_Guid, Workorderstep_Guid, oldHandleby_Id, oldHandlegroup_Id, Workorderstepstatus, Comments, Modifieddate, From_Stepidmap, TO_STEPIDMAP, Workstarttime, Workendtime,CHILD_ID) " + "Values ('" + in_s_objective_guid + "', uuid(), '" + operate_staff + "', '" + operate_group + "','FAILED', '" + comments + "', now(), '" + CURSTEP_ID_MAP + "', '" + CURSTEP_ID_MAP + "', now(), now(),'手机端')";

            log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
            int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

            result.put("code", "1");
            result.put("msg", "转派成功")
        }
        return result;
    }

    //重新提交
    JSONObject reSubmitOrder(EliteBean eliteBean, JSONObject tabcValue, String operation_type) {
        log.debug("---------------------进入工单重新提交逻辑---------------------");
        Map fieldValue = new HashMap<String, Object>();
        JSONObject result = new JSONObject();
        //取主键
        String tabctablename = tabcValue.getString("tabctablename"); //工单tabc表名
        String in_s_objective_guid = tabcValue.getString("in_s_objective_guid"); //当前objective_guid
        String in_s_elitetask_guid = tabcValue.getString("in_s_elitetask_guid"); //当前elitetask_guid
        //取操作信息
        String comments = tabcValue.getString("comments"); //操作备注
        String operate_group = tabcValue.getString("operate_group"); //操作组
        String operate_staff = tabcValue.getString("operate_staff"); //操作人
        String handleby_id = tabcValue.getString("handleby_id"); //给处理人
        String handlegroup_id = tabcValue.getString("handlegroup_id"); //给处理组

        //取需修改tabc字段
        JSONObject tabcJSON = tabcValue.getJSONObject("tabc_info");
        fieldValue.putAll(tabcJSON);
        String fieldName = "";
        String valueString = "";
        String updateString = "";
        int i = 0;
        List<Object> valueList = new ArrayList<Object>();
        for (String key : fieldValue.keySet()) {
            log.debug("key:" + key);
            if (!KHUtil.isNull(String.valueOf(fieldValue.get(key)))) {
                updateString = updateString + "," + key + "=" + "'" + fieldValue.get(key) + "'";
                insertString = insertString + "," + key;
            }

            if (!String.valueOf(fieldValue.get(key)).equals("null")) {
                valueList.add(KHUtil.isNull(String.valueOf(fieldValue.get(key))) ? null : fieldValue.get(key));
            } else {
                valueList.add("'" + fieldValue.get(key) + "'");
            }
            i++;
        }

        updateString = updateString.replaceFirst(",", "");
        insertString = insertString.replaceFirst(",", "");

        log.debug("updateString:" + updateString);
        log.debug("insertString:" + insertString);

        //查询当前数据信息
        List workinfolist = eliteBean.chaxun_sql("select handleby_id,handlegroup_id,MAP_ID,CURSTEP_ID_MAP from objective where objective_guid='" + in_s_objective_guid + "'", "dataSource")
        //当前工单信息
        if (workinfolist.size() > 0) {
            String[] data1 = new String[5];
            data1[0] = "update objective set objectivestatus='OPEN',HANDLEBY_ID='" + handleby_id + "',HANDLEGROUP_ID='" + handlegroup_id + "',OBJECTIVESUBSTATUS='OPEN',STRINGFIELD2='" + handleby_id + "'  where objective_guid='" + in_s_objective_guid + "'";
            data1[1] = "INSERT INTO elitetask (ELITETASK_GUID,OBJECTIVE_GUID,OBJECTIVETYPE_ID,PRIORITY,HANDLEBY_ID,HANDLEGROUP_ID,CREATEDBY,CREATEDDATE,MODIFIEDBY,MODIFIEDDATE,COMMENTS,CUSTOMER_GUID,WORKORDERFRAME_ID,CURSTEPSTATUS,CURSTEP_ID,CURSTEP_ID_MAP,MAP_ID,OBJECTIVESUBSTATUS,HANDLEROLE_ID) SELECT uuid(),o.objective_guid,o.OBJECTIVETYPE_ID,o.priority_id,'" + handleby_id + "','" + handlegroup_id + "',o.CREATEDBY,o.CREATEDDATE,o.MODIFIEDBY,o.MODIFIEDDATE,o.COMMENTS,o.CUSTOMER_GUID,o.WORKORDERFRAME_ID,o.objectivesubstatus,o.curstep_id_map,o.curstep_id_map,o.map_id,o.objectivesubstatus,o.HANDLEROLE_ID FROM OBJECTIVE o WHERE o.OBJECTIVE_GUID = '" + in_s_objective_guid + "'";
            data1[2] = "INSERT INTO workorderstep3 (WORKORDER_GUID,WORKORDERSTEP_GUID,OLDHANDLEBY_ID,OLDHANDLEGROUP_ID,HANDLEBY_ID,HANDLEGROUP_ID,WORKORDERSTEPSTATUS,COMMENTS,MODIFIEDDATE,FROM_STEPIDMAP,TO_STEPIDMAP) select o.objective_guid,uuid(),'" + operate_staff + "','" + operate_group + "','" + handleby_id + "','" + handlegroup_id + "','" + operation_type + "','" + comments + "',now(),o.curstep_id_map,o.curstep_id_map from objective o where objective_guid='" + in_s_objective_guid + "'";
            data1[3] = "update " + tabctablename + " set " + updateString + " where relation_id='" + in_s_objective_guid + "'";
            data1[4] = "insert into tabcworkorder_his select * from tabcworkorder where relation_id='" + in_s_objective_guid + "'";

            log.debug("判断处理工单状态进行下一步处理SQL:" + data1.toString());
            int[] dataresult1 = eliteBean.updateBatch(data1, "dataSource");

            createMessageTask(eliteBean, in_s_objective_guid, handleby_id, "", operation_type, "", operate_staff, "", "", "", "");


            result.put("code", "1");
            result.put("msg", "重新提交成功");
        }
        return result;
    }


    // 处理JSON字符串中的特殊字符
    static String filterJson(String inData) {
        List<String> filterList = new ArrayList<>();
        filterList.add("\\\\");
        filterList.add("¥");
        filterList.add("@");
        filterList.add("\b");
        filterList.add("\f");
        filterList.add("\n");
        filterList.add("\n");
        filterList.add("\t");

        log.debug("处理JSON字符串中的特殊字符-前:" + inData);
        for (int i = 0; i < filterList.size(); i++) {
            inData = inData.replaceAll(filterList.get(i), "");
        }
        log.debug("处理JSON字符串中的特殊字符-中:" + inData);
        inData = inData.replaceAll("'", "''");
        inData = toJsonString(inData);
        log.debug("处理JSON字符串中的特殊字符-后:" + inData);

        // inData = toJson2String(inData);
        return inData;
    }

    // 处理json字符串中value多余的双引号， 将多余的双引号替换为中文双引号
    private static String toJsonString(String s) {
        char[] tempArr = s.toCharArray();
        int tempLength = tempArr.length;
        for (int i = 0; i < tempLength; i++) {
            if (tempArr[i] == ':' && tempArr[i + 1] == '"') {
                for (int j = i + 2; j < tempLength; j++) {
                    if (tempArr[j] == '"') {
                        if (tempArr[j + 1] != ',' && tempArr[j + 1] != '}') {
                            tempArr[j] = '”'; // 将value中的 双引号替换为中文双引号
                        } else if (tempArr[j + 1] == ',' || tempArr[j + 1] == '}') {
                            break;
                        }
                    }
                }
            }
        }
        return new String(tempArr);
    }

    //获取工单流程相关信息及获取工单流程中的下个步骤处理人（后续可增加多任务、超时配置信息）
    static List getWorkProcess(EliteBean eliteBean, String map_id, String source_id, String objectivesubstatus) {
        List<Map<String, String>> dataList = new ArrayList<>();

        String getWorkProcess = "select wm.map_id,wn.nodemap_id,wn.nodemapname,wm.workorderframe_id,wn.priority_id,wn.handlegroup_id,wn.handleby_id,wm.source_id,wm.target_id,wn.handletype,wn.sendtype,wn.defsub,ws.objectivesubstatus_id,wm.alwaysopen,w1.nodemap_id as s2_nodemap from wo3_link_map wm left join wo3_node_map wn on wm.map_id=wn.map_id and wm.target_id=wn.nodemap_id left join wo3_linkmap_substatus ws on wm.MAP_ID=ws.MAP_ID  AND WM.LINKMAP_ID=WS.LINKMAP_ID left join wo3_nodemap_extend w1 on wm.map_id=w1.MAP_ID and wm.target_id = w1.nodemap_id and EXTENDID='nodetype' and EXTENDVALUE='S2' where wm.map_id='" + map_id + "' and wm.SOURCE_ID='" + source_id + "'"
        log.debug("步骤查询SQL" + getWorkProcess);
        List workProcessList = eliteBean.chaxun_sql(getWorkProcess, "dataSource");
        log.debug(workProcessList.toString())
        String target_id = "";
        String handleType = "";
        String sendType = "";
        String handlerole_id = "";
        String handlegroup_id = "";
        String handleby_id = "";
        String workorderframe_id = "";
        String defsub = "";

        for (int i = 0; i < workProcessList.size(); i++) {
            String alwaysopen = workProcessList.get(i).get("alwaysopen");//无条件开通
            String objectivesubstatus_id = workProcessList.get(i).get("objectivesubstatus_id");//工单下个步骤ID
            String s2_nodemap = workProcessList.get(i).get("s2_nodemap");//是否业务处理节点 S2时该值不为空
            if (alwaysopen == "1" || objectivesubstatus_id == objectivesubstatus) {
                target_id = workProcessList.get(i).get("target_id");//工单下个步骤ID
                workorderframe_id = workProcessList.get(i).get("workorderframe_id");//工单模板ID
                handleType = workProcessList.get(i).get("handletype");//工单下个步骤责任对象
                sendType = workProcessList.get(i).get("sendtype");//工单下个步骤下送对象类型
                defsub = workProcessList.get(i).get("defsub");//工单下个步骤节点默认状态
                String re_handlegroup_id = workProcessList.get(i).get("handlegroup_id");//工单下个步骤处理组
                String re_handleby_id = workProcessList.get(i).get("handleby_id");//工单下个步骤处理人

                //责任对象=组+角色,下送指定=组+角色
                //根据组和角色查找处理人。查找到部门角色只有一个则指定到人，多个则指定到部门+角色。由部门下的该角色成员查询接管处理。
                //工单通知需要通知到角色对应的所有人
                if (handleType == "GR" && sendType == "10") {
                    handlerole_id = re_handleby_id;
                    handlegroup_id = re_handlegroup_id;
                    handleby_id = "";
                }

                //责任对象=角色,下送指定=角色
                //根据角色查找处理人。查找角色只有一个人则指定到人，多个则指定到角色。由该角色成员查询接管处理。
                //工单通知需要通知到角色对应的所有人
                if (handleType == "R" && sendType == "3") {
                    handlerole_id = re_handlegroup_id;
                    handlegroup_id = "";
                    handleby_id = "";
                }

                //责任对象=部门,下送指定=部门
                //根据部门查找对应处理人。查找到部门下只有一个人则指定到人。多个则指定到部门。由部门成员接管处理
                //工单通知需要通知到部门所有人
                if (handleType == "G" && sendType == "1") {
                    handlerole_id = "";
                    handlegroup_id = re_handlegroup_id;
                    handleby_id = "";
                }

                //责任对象=部门,下送指定=部门+人
                //直接指定到人。
                //工单通知需要通知到指定人
                if (handleType == 'G' && sendType == '2') {
                    handlerole_id = ''
                    handlegroup_id = re_handlegroup_id;
                    handleby_id = re_handleby_id;
                }

                //返回工单流程节点数据
                Map<String, String> map = new HashMap<>();
                map.put("target_id", target_id);
                map.put("workorderframe_id", workorderframe_id);
                map.put("handletype", handleType);
                map.put("sendtype", sendType);
                map.put("defsub", defsub);
                map.put("handlerole_id", handlerole_id);
                map.put("handlegroup_id", handlegroup_id);
                map.put("handleby_id", handleby_id);
                map.put("s2_nodemap", s2_nodemap);
                dataList.add(map);
                log.debug(dataList.toString());
            }
        }
        return dataList
    }

    //创建消息通知,in_operation_type=操作类型
    void createMessageTask(EliteBean eliteBean, String objective_guid, String handleby_id, String handle_role, String send_type, String comments, String operate_staff, String map_id, String province, String city, String area) {
        log.debug("处理消息通知：" + objective_guid + "|" + handleby_id + "|" + handle_role);
        //处理人不为空就优先通知到处理人
        String getQwidSQL = "";
        List QwList = null;
        if (handleby_id != "") {
            getQwidSQL = "select distinct userid from staff where staff_id='" + handleby_id + "'";//企业微信id
            QwList = eliteBean.chaxun_sql(getQwidSQL, "DataSource");
        } else if (handleby_id == "" && (handle_role == "BWUT04" || handle_role == "C00001" || handle_role == "BWUT02" || handle_role == "BWUT05" || handle_role == "BWUT03" || handle_role == "BWUT01" || handle_role == "BWUT09" || handle_role == "CWUT01")) {
            getQwidSQL = "select distinct s.userid from dg_businessstaff db left join dg_businessstaff_list df on db.rec_guid=df.batch_guid left join staff s on df.staff_id=s.staff_id  where db.business_type='" + handle_role + "' And s.Userid Is Not Null And s.Userid != '' ";
            QwList = eliteBean.chaxun_sql(getQwidSQL, "DataSource");
        } else if (handleby_id == "" && handle_role == "AWUT05") { //假货举报，按照省通知对应的维权人员
            getQwidSQL = "select distinct s.userid from dg_businessstaff db left join dg_businessstaff_list df on db.rec_guid=df.batch_guid left join staff s on df.staff_id=s.staff_id left join tabcworkorder tabc on db.province=tabc.province and tabc.RELATION_ID='" + objective_guid + "' where db.business_type='" + handle_role + "' And s.Userid Is Not Null And s.Userid != '' and tabc.RELATION_ID='" + objective_guid + "'";
            QwList = eliteBean.chaxun_sql(getQwidSQL, "DataSource");
        }

        JSONArray recipient_person = new JSONArray();
        if (QwList.size() > 0) {
            for (int i = 0; i < QwList.size(); i++) {
                recipient_person.add(QwList.get(i).get("userid"))
            }
        } else {
            log.debug("未获取到企业微信ID")
        }
        log.debug(recipient_person.toString());//["xxx","xxx"]

        JSONObject reqJson = new JSONObject();
        JSONArray body = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("objective_guid", objective_guid);//工单编号
        json1.put("recipient_person", recipient_person);//通知人
        body.add(json1);
        reqJson.put("body", body);
        reqJson.put("comments", comments);
        reqJson.put("send_type", send_type);
        reqJson.put("operate_staff", operate_staff);
        log.debug(reqJson.toString());

        String apiLinkJson = Util.doHttpPost(KHDBConfig.getAPIURL(eliteBean, "KFZX_WorkMessage"), reqJson.toString(), "UTF-8", 5000, 0);
        log.debug(apiLinkJson);

    }

}